package com.osmino.contacts.list;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.devoteam.quickaction.QuickActionWindow;
import com.devoteam.quickaction.QuickItem;
import com.osmino.contacts.R;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.entity.QuickItemInflater;
import com.osmino.core.style.IStyleableView;
import com.osmino.core.style.Style;
import com.osmino.core.utils.view.Backgroung;

public class ContactItem extends RelativeLayout implements IStyleableView, Checkable {

	private final boolean DBG = true;
	private final int LAYOUT = R.layout.contact_item;
	private final int BG_ONE = R.drawable.base_list_bg_1;
	private final int BG_TWO = R.drawable.base_list_bg_2;
	private static final int PIC_DEF = R.drawable.base_no_avatar_ic;
	private final ImageView mCheckbox;
	private final ImageView mBadge;
	private final TextView mLabel;
	private boolean mToggleShown = false;
	private boolean mChecked = false;
	private boolean mOdd;
	private static final int colorGreen = Color.parseColor("#98b933");
	private static final int RIGHT_CHECKBOX_MARGIN_NO_ABC = 22;
	private static final int RIGHT_CHECKBOX_MARGIN_ABC = 42;

	public ContactItem(Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(LAYOUT, this);
		mCheckbox = (ImageView) findViewById(R.id.contact_item_checkbox);
		mBadge = (ImageView) findViewById(R.id.contact_item_pic_iv);
		mLabel = (TextView) findViewById(R.id.contact_item_text_tv);
		setChecked(false);
	}

	public void clearInfo() {
		mLabel.setText("");
		mBadge.setImageResource(PIC_DEF);
	}

	public void setBackground(boolean isOdd) {
		mOdd = isOdd;
		handleCheckAndOrder();
	}

	public void setInfo(final Contact contact, String filterSequence) {
		clearInfo();
		String displayName = contact.displayName;
		SpannableStringBuilder ssb = new SpannableStringBuilder(displayName);
		if (filterSequence != null && !filterSequence.equals("")) {
			int start = displayName.indexOf(filterSequence);
			if (start < 0) {
				start = 0;
			}
			int end = start + filterSequence.length();
			ssb.setSpan(new ForegroundColorSpan(colorGreen), start, end, Editable.SPAN_INCLUSIVE_INCLUSIVE);
		}
		mLabel.setText(ssb);
		if (contact.getPhoto() != null) {
			mBadge.setImageDrawable(new BitmapDrawable(getResources(), contact.getPhoto()));
		} else {
			mBadge.setImageResource(PIC_DEF);
		}
		
		//Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contact.id);
		//TODO Nikolay Litvyakov
		List<QuickItem> items = QuickItemInflater.makeQuickActionList(getContext(), contact);
		//TODO fill items using Contact contact
		QuickActionWindow.setAction(mBadge, items);
	}

	public void setMarginLeft(int marginLeft) {
		RelativeLayout.LayoutParams lp = (LayoutParams) mBadge.getLayoutParams();
		if (lp == null) {
			lp = new RelativeLayout.LayoutParams(36, 36);

		}
		lp.setMargins(marginLeft, 0, 0, 0);
		mBadge.setLayoutParams(lp);
	}

	@Override
	public void setStyle(Style style) {
	}

	public void showToggle(boolean isShown) {
		mToggleShown = isShown;
		mCheckbox.setVisibility(mToggleShown ? VISIBLE : INVISIBLE);
	}

	@Override
	public boolean isChecked() {
		return mChecked;
	}

	@Override
	public void setChecked(boolean checked) {
		mChecked = checked;
		mCheckbox.setBackgroundResource(mChecked ? R.drawable.base_list_checkbox_checked
				: R.drawable.base_list_checkbox);
		handleCheckAndOrder();
	}
	
	private void handleCheckAndOrder() {
		if (mChecked && mToggleShown) {
			findViewById(R.id.contact_item_bg_rl).setBackgroundColor(Color.parseColor("#ced0df"));
		} else {
			int colorWhite = Color.rgb(255, 255, 255);
			int colorLightGray = Color.rgb(247, 247, 247);
			int colorAct = Color.rgb(200, 200, 200);
			if (mOdd) {
				Backgroung.setBackgroundColor(findViewById(R.id.contact_item_bg_rl), colorWhite, colorAct);
			} else {
				Backgroung.setBackgroundColor(findViewById(R.id.contact_item_bg_rl), colorLightGray, colorAct);
			}			
		}
	}

	@Override
	public void toggle() {
		setChecked(!mChecked);
	}
	
	public void setAlphabetIndexerOffset(boolean isAlphabetIndexer){
		Log.e("ContactItem", "isAlphabetIndexer:" + isAlphabetIndexer);
		ImageView v = (ImageView) findViewById(R.id.contact_item_checkbox);
		RelativeLayout.LayoutParams rllp = (LayoutParams) v.getLayoutParams();
		int wp = RelativeLayout.LayoutParams.WRAP_CONTENT;
		if(rllp==null){
			rllp = new RelativeLayout.LayoutParams(wp, wp);
		}
		if(!isAlphabetIndexer){
			rllp.setMargins(0, 0, RIGHT_CHECKBOX_MARGIN_NO_ABC, 0);
		}
		else{
			rllp.setMargins(0, 0, RIGHT_CHECKBOX_MARGIN_ABC, 0);
		}
		v.setLayoutParams(rllp);
	}
}